<template>
  <div>
    <!--    <div class="toolbar">-->
    <!--      <el-form :inline="true" :model="q" size="mini">-->
    <!--        <el-form-item label="渠道商查询:">-->
    <!--          <el-input v-model="q.name" placeholder="渠道商" prefix-icon="el-icon-edit"></el-input>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item>-->
    <!--          <el-button type="primary" @click="query">查询</el-button>-->
    <!--          <el-button type="danger" @click="reload">重置</el-button>-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->
    <!--    </div>-->
    <el-row type="flex" class="row-bg btn-row" justify="end">
      <el-button size="mini" @click="add" type="primary" icon="el-icon-plus">新增商户</el-button>
    </el-row>
    <el-table :data="listData.items" v-loading.body="listLoading" @sort-change="sortlist" size="mini" stripe border fit
              highlight-current-row>
      <el-table-column prop="id" label="商户id" width="80" fixed="left"></el-table-column>
      <el-table-column prop="name" label='商户名称' sortable min-width="160"></el-table-column>
      <el-table-column prop="mch_type" label='商户类型' width="160">
        <template slot-scope="scope">{{MchType[scope.row.mch_type]}}</template>
      </el-table-column>
      <el-table-column prop="create_order_url" label='订单地址' min-width="180"></el-table-column>
      <el-table-column prop="recharge_order_url" label='充值地址' min-width="180"></el-table-column>
      <el-table-column prop="chase_order_url" label='追分地址' min-width="180"></el-table-column>
      <el-table-column prop="get_charge_info_url" label='充值查询地址' min-width="180"></el-table-column>
      <el-table-column prop="fail_order_url" label='订单失败地址' min-width="180"></el-table-column>

      <el-table-column prop="enable" label='商户状态' min-width="80" :formatter="formartToText"></el-table-column>
      <el-table-column prop="created_at" label="创建时间" sortable width="140"></el-table-column>
      <el-table-column prop="updated_at" label='最后更新时间' width="140"></el-table-column>

      <el-table-column label="操作" width="160" fixed="right">
        <template slot-scope="scope">
          <div>
            <el-button :type="scope.row.enable?'warning':'info'" size="mini" @click="enableOrdisableMch(scope.row)">
              {{scope.row.enable?'禁用':'启用'}}
            </el-button>
            <el-button type="primary" size="mini" @click="update(scope.row)">编辑</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="q.page_no" :page-sizes="$store.state.app.pageSizeArr"
                     :page-size="q.page_size"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="listData.total">
      </el-pagination>
    </div>
    <save-update v-if="showUpdate" :showUpdate.sync="showUpdate" :id="show_id" :MerchantType="MerchantType"
                 :settleCycle="settleCycle"></save-update>
  </div>
</template>

<script>
  import saveUpdate from './saveUpdate'
  import {getMerchantsList, deleteMerchant, disableMch, enableMch} from '@/api'
  import {useVcode} from "../../utils/CommonJs";

  export default {
    components: {saveUpdate},
    data() {
      return {
        listData: {},
        q: {page_size: 30, page_no: 1},
        listLoading: false,
        showUpdate: false,
        settleCycle: this.$store.state.app.settleCycle,
        MchType: this.$store.state.app.MchType,
        show_id: null,
        MerchantType: this.$store.state.app.MerchantType
      }
    },
    activated() {
      this.getList()
    },
    deactivated() {
      this.listLoading = false
    },
    methods: {
      add() {
        this.show_id = null
        this.showUpdate = true
      },
      resetPwd(row) {
        this.$message.warning('待开发')
      },
      update(row) {
        this.show_id = row.id
        this.showUpdate = true
      },
      remove(row) {
        this.$confirm(`确认删除ID为${row.id}的项么？`, '删除代理', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteMerchant(row.id).then(res => {
            if (res.status === 0) {
              this.$message({type: 'success', message: '删除成功!'})
              this.getList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      query() {
        this.q = Object.assign(this.q, {page_no: 1, page_size: 30})
        this.getList()
      },
      reload() {
        this.q = {page_size: 30, page_no: 1}
        this.getList()
      },
      sortlist(sort) {
        this.q.$orderby = sort.prop
        this.getList()
      },
      handleSizeChange(val) {
        this.q.page_size = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.q.page_no = val
        this.getList()
      },
      getList() {
        getMerchantsList(this.q).then(res => {
          if (res.status === 0) {
            this.listLoading = false
            this.listData = res.data
          }
        })
      },
      formartToText(row, column) {
        switch (column.property) {
          case 'enable':
            return row.enable ? '开启' : '禁用'
        }
      },
      enableOrdisableMch(row) {
        if (row.enable) {
          this.$confirm(`确认禁用id为${row.id}的商户？`, "禁用确认",
            {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }
          ).then(() => {
            useVcode(this, () => {
              disableMch(row.id).then(res => {
                if (res.status === 0) {
                  row.enable = !row.enable
                }
              })
            })
          })
        } else {
          this.$confirm(`确认启用id为${row.id}的商户？`, "启用确认",
            {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }
          ).then(() => {
            useVcode(this, () => {
              enableMch(row.id).then(res => {
                if (res.status === 0) {
                  row.enable = !row.enable
                }
              })
            })
          })

        }
      }
    },
    watch: {
      showUpdate() {
        if (!this.showUpdate) {
          this.getList()
        }
      }
    }
  }
</script>
